package com.hgyc.mom.wms.service.impl;

import com.hgyc.mom.common.enums.FinishTypeEnum;
import com.hgyc.mom.wms.vo.CrmSalesOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hgyc.mom.wms.mapper.CrmSalesOrderMapper;
import com.hgyc.mom.wms.entity.CrmSalesOrder;
import com.hgyc.mom.wms.entity.CrmSalesDetail;
import com.hgyc.mom.wms.service.CrmSalesDetailService;
import com.hgyc.mom.wms.service.CrmSalesOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import com.hgyc.mom.common.exception.BizException;
import org.springframework.util.CollectionUtils;

/**
 * 销售订单Service业务层实现
 *
 * @author fwj
 * @since 2025-05-19
 */
@Slf4j
@Service
public class CrmSalesOrderServiceImpl extends ServiceImpl<CrmSalesOrderMapper, CrmSalesOrder> implements CrmSalesOrderService {

    @Autowired
    private CrmSalesDetailService crmSalesDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSaleOrder(CrmSalesOrderVO crmSalesOrderVO) {
        // TODO 其他逻辑
        save(crmSalesOrderVO);

        //新增销售订单明细
        List<CrmSalesDetail> saleDetails = crmSalesOrderVO.getAddSaleDetails();
        if (!CollectionUtils.isEmpty(saleDetails)) {
            saleDetails.forEach(item -> {
                item.setId(null);
                item.setOrderId(crmSalesOrderVO.getId());
                item.setStatus(FinishTypeEnum.UNFINISHED.getValue());
            });
            crmSalesDetailService.saveBatch(saleDetails);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSaleOrder(CrmSalesOrderVO crmSalesOrderVO) {
        // TODO 其他逻辑
        updateById(crmSalesOrderVO);

        //删除销售订单明细
        if (!CollectionUtils.isEmpty(crmSalesOrderVO.getDeleteSaleDetailIds())) {
            crmSalesDetailService.removeBatchByIds(crmSalesOrderVO.getDeleteSaleDetailIds());
        }

        //新增销售订单明细
        List<CrmSalesDetail> saleDetails = crmSalesOrderVO.getAddSaleDetails();
        if (!CollectionUtils.isEmpty(saleDetails)) {
            saleDetails.forEach(item -> {
                item.setId(null);
                item.setOrderId(crmSalesOrderVO.getId());
                item.setStatus(FinishTypeEnum.UNFINISHED.getValue());
            });
            crmSalesDetailService.saveBatch(saleDetails);
        }

        //更新销售订单明细
        if (!CollectionUtils.isEmpty(crmSalesOrderVO.getUpdateSaleDetails())) {
            List<CrmSalesDetail> updateSaleDetails = crmSalesOrderVO.getUpdateSaleDetails();
            crmSalesDetailService.updateBatchById(updateSaleDetails);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeSaleOrder(List<Long> ids) {
        // TODO 其他逻辑
        removeByIds(ids);
        //删除销售订单明细
        crmSalesDetailService.deleteBatchByParentIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<CrmSalesOrder> crmSalesOrderList, boolean updateSupport) {
        if (crmSalesOrderList == null || crmSalesOrderList.isEmpty()) {
            throw new IllegalArgumentException("导入数据不能为空");
        }

        try {
            if (updateSupport) {
                // TODO 实现更新已有数据的导入逻辑
                return "更新导入成功，共导入 " + crmSalesOrderList.size() + " 条数据";
            } else {
                // TODO 实现不更新已有数据的导入逻辑
                return "新增导入成功，共导入 " + crmSalesOrderList.size() + " 条数据";
            }
        } catch (Exception e) {
            log.error("销售订单导入失败", e);
            throw new BizException("销售订单导入失败：" + e.getMessage());
        }
    }
}